<?php

namespace Home\Controller;

/**
 * Description of InterfaceServerController.class控制器实例
 * 
 * @author wenhui
 * @date 2016-1-4
 */
class InterfaceServerController extends ParentController {

    /**
     * @description 接口服务视图页面
     * @modify wenhui 2017-11-09
     */
    public function index() {
        $serverModel = D('Server');
        $serverInfoModel = D('ServerInfo');

        $interfaceServerInfo = $serverModel->field('id,sign')->select();
        $interfaceServerInfo = getArrayValueAsIndex($interfaceServerInfo, 'id');
        $serverIds = array_column($interfaceServerInfo, 'id');

        if (!empty($serverIds)) {
            $serverInfo = $serverInfoModel->field('id,server_id,language,name,description')->where(['server_id' => ['in', $serverIds]])->select();
        } else {
            $serverInfo = array();
        }
        // 组装数据
        foreach ($serverInfo as $key => $serverItem) {
            if (isset($interfaceServerInfo[$serverItem['server_id']])) {
                if ($serverItem['language'] == 'zh_cn') {
                    $interfaceServerInfo[$serverItem['server_id']]['name'] = $serverItem['name'];
                    $interfaceServerInfo[$serverItem['server_id']]['description'] = $serverItem['description'];
                }
                $interfaceServerInfo[$serverItem['server_id']]['language'][] = $serverItem;
            }
        }
        unset($serverItem);
        $interfaceServerInfo = array_values($interfaceServerInfo);

        // 分页

        $this->assign('serverInfo', $interfaceServerInfo);
        $this->display();
    }

    /**
     * @action 添加产品
     */
    public function addInterfaceServer() {
        $zhname = I('post.zhname', '', 'string');
        $twname = I('post.twname', '', 'string');
        $enname = I('post.enname', '', 'string');
        $zhdescription = I('post.zhdescription', '', 'string');
        $twdescription = I('post.twdescription', '', 'string');
        $endescription = I('post.endescription', '', 'string');
        $sign = I('post.sign', '', 'string');
        $vcode = I('post.vcode', '', 'string');

        if (!checkVcode($vcode)) {
            returnJson(403, 201, '验证码不正确', 'error');
        }

        $serverModel = D('Server');
        $serverInfoModel = D('ServerInfo');
        $serverModel->startTrans();
        $commit = true;

        $sign = trim($sign);
        $serverId = $addResult = $serverModel->addInfo($zhname, $sign);
        if ($addResult == false) {
            $commit = false;
        }

        if ($commit) {
            $data = array(
                array(
                    'server_id' => $serverId,
                    'language' => 'zh_cn',
                    'name' => $zhname,
                    'description' => $zhdescription,
                ),
                array(
                    'server_id' => $serverId,
                    'language' => 'zh_tw',
                    'name' => $twname,
                    'description' => $twdescription,
                ),
                array(
                    'server_id' => $serverId,
                    'language' => 'en_us',
                    'name' => $enname,
                    'description' => $endescription,
                ),
            );
            $addResult = $serverInfoModel->addInfo($data);
            if ($addResult == false) {
                $commit = false;
            }
        }

        if ($commit) {
            $serverModel->commit();
            returnJson(200, 200, '添加成功', 'success');
        } else {
            $serverModel->rollback();
            returnJson(500, 508, '添加失败', 'error');
        }
    }

    /**
     * @action 修改产品
     */
    public function saveInterfaceServer() {
        $zhname = I('post.zhname', '', 'string');
        $twname = I('post.twname', '', 'string');
        $enname = I('post.enname', '', 'string');
        $zhdescription = I('post.zhdescription', '', 'string');
        $twdescription = I('post.twdescription', '', 'string');
        $endescription = I('post.endescription', '', 'string');
        $serverId = I('post.serverId', '', 'intval');
        $vcode = I('post.vcode', '', 'string');
        $sign = I('post.sign', '', 'string');

        if (!checkVcode($vcode)) {
            returnJson(403, 201, '验证码不正确', 'error');
        }

        $serverModel = D('Server');
        $serverInfoModel = D('ServerInfo');
        $serverModel->startTrans();
        $commit = true;

        $sign = trim($sign);
        $saveResult = $serverModel->saveInfo($serverId, $zhname, $sign);
        if ($saveResult == false) {
            $commit = false;
        }

        if ($commit) {
            $data = array(
                array(
                    'language' => 'zh_cn',
                    'name' => $zhname,
                    'description' => $zhdescription,
                ),
                array(
                    'language' => 'zh_tw',
                    'name' => $twname,
                    'description' => $twdescription,
                ),
                array(
                    'language' => 'en_us',
                    'name' => $enname,
                    'description' => $endescription,
                ),
            );
            foreach ($data as $key => $dataItem) {
                $saveResult = $serverInfoModel->saveInfo($serverId, $dataItem['language'], $dataItem['name'], $dataItem['description']);
                if ($saveResult == false) {
                    $commit = false;
                    break;
                }
            }
        }

        if ($commit) {
            $serverModel->commit();
            returnJson(200, 200, 'success');
        } else {
            $serverModel->rollback();
            returnJson(500, 506, 'error');
        }
    }

    /**
     * @action 修改产品
     */
    /*public function getInterfaceServer() {
        $serverModel = D('Server');
        $serverInfoModel = D('ServerInfo');

        $serverInfo = $serverModel->select();
        $serverIds = array_column($serverInfo, 'id');

        $serverInfo = $serverInfoModel->field('id,server_id,language,name')->where(['language'=>'zh_cn', 'server_id'=>['in', $serverIds]])->select();

        returnJson(200, 200, $serverInfo);
    }*/
}
